﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data.Objects.DataClasses;
using BaseUtilities;

namespace Controller.DataProvider
{
    public class ProductTypePro : GenericProvider<ProductType>
    {
        public ProductTypePro()
        {
            EntityName = "Tbl_ProductType";
        }

        #region override Methods
        //----------------------------------------
        public override EntityObject CreateEntity()
        {
            return new Tbl_ProductType();
        }
        //----------------------------------------
        public override ProductType CreateModel()
        {
            return new ProductType();
        }
        //----------------------------------------
        public override void ToEntity(ProductType pModel, EntityObject pEntity, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_ProductType oEntity = pEntity as Tbl_ProductType;
            oEntity.ParentId = pModel.ParentId;
            oEntity.Code = pModel.Code;
            oEntity.Name = pModel.Name;
            oEntity.Note = pModel.Note;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        oEntity.Id = pModel.Id;
                        break;
                    case EnumFieldCopy.Created:
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                    case EnumFieldCopy.All:
                        oEntity.Id = pModel.Id;
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                }
            });
        }
        //----------------------------------------
        public override void ToModel(EntityObject pEntity, ProductType pModel, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_ProductType oEntity = pEntity as Tbl_ProductType;
            pModel.ParentId = oEntity.ParentId;
            pModel.Code = oEntity.Code;
            pModel.Name = oEntity.Name;
            pModel.Note = oEntity.Note;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        pModel.Id = oEntity.Id;
                        break;
                    case EnumFieldCopy.Created:
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        break;
                    case EnumFieldCopy.Relation:
                        if (oEntity.Tbl_ProductType2 != null)
                            pModel.ParentName = oEntity.Tbl_ProductType2.Name;
                        break;
                    case EnumFieldCopy.All:
                        pModel.Id = oEntity.Id;
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        if (oEntity.Tbl_ProductType2 != null)
                            pModel.ParentName = oEntity.Tbl_ProductType2.Name;
                        break;
                }
            });
        }
        #endregion override Methods
        //----------------------------------------
        public ResultModel GetAllChilds(int? pParentId)
        {
            IList<ProductType> result = new List<ProductType>();
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                List<Tbl_ProductType> lst = entities.Tbl_ProductType.Where(p => pParentId.HasValue ? p.ParentId == pParentId : !p.ParentId.HasValue).ToList();
                lst.ForEach((item) =>
                {
                    ProductType oProductType = new ProductType();
                    ToModel(item, oProductType, EnumFieldCopy.All);
                    result.Add(oProductType);
                });
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetAllLeafs()
        {
            IList<ProductType> result = new List<ProductType>();
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                List<Tbl_ProductType> lst = entities.Tbl_ProductType.Where(p => p.Tbl_ProductType1.Count == 0).ToList();
                lst.ForEach((item) =>
                {
                    ProductType oProductType = new ProductType();
                    oProductType.Id = item.Id;
                    ToModel(item, oProductType, EnumFieldCopy.All);
                    result.Add(oProductType);
                });
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel CheckProductTypeCode(int pId, string pCode)
        {
            bool result = false;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = entities.Tbl_ProductType.Where(p => p.Id != pId && string.Compare(p.Code, pCode, true) == 0).FirstOrDefault() != null;
            }
            return new ResultModel(result);
        }
    }
}
